const db = require("../db/index");
// 导入加密中间件
const bcrypt = require("bcryptjs");
const fs = require("fs");

const adminController = {
  // 添加产品管理员
  addAdmin: async (req, res) => {
    const { account, password, name, sex, department, email, identity } =
      req.body;
    //   判断有无账号
    const sql = "SELECT * FROM users WHERE account = ?";
    // db.query(sql, regInfo.account, (err, result) => {
    const [data] = await db.query(sql, account);
    if (data.length) {
      return res.send({
        status: -1,
        message: "账号已存在",
      });
    }
    // 加密密码
    const hashPassword = bcrypt.hashSync(password, 10);
    // 账号密码加入数据库
    const sql1 = "INSERT INTO users SET ?";
    const create_time = new Date();
    try {
      await db.query(sql1, {
        // 用户名
        account,
        // 密码
        password: hashPassword,
        name,
        sex,
        department,
        email,
        // 身份
        identity,
        // 创建时间
        create_time,
        // 是否冻结  1 - 未冻结
        status: 1,
      });
    } catch (err) {
      res.cc(err);
    }
    res.send({
      status: 1,
      message: "添加成功",
    });
  },
  // 获取管理员列表
  getAdminList: async (req, res) => {
    const { identity, page } = req.query;
    const sql = `SELECT * FROM users WHERE identity = ? ORDER BY create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM users WHERE identity = ?`;
    const [data] = await db.query(sql, identity);
    const [total] = await db.query(sql1, identity);
    res.send({
      status: 1,
      message: "成功",
      data: data,
      total: total.length,
    });
  },
  // 编辑管理员信息
  updateAdmin: async (req, res) => {
    const { id, name, sex, email, department } = req.body;
    const update_time = new Date();
    const sql1 = "SELECT department FROM users WHERE id = ?";
    const [data] = await db.query(sql1, id);
    if (data[0].department === department) {
      const updateContent = {
        name,
        sex,
        email,
        department,
        update_time,
      };
      const sql = "UPDATE users SET ? WHERE id = ?";
      try {
        await db.query(sql, [updateContent, id]);
        res.send({
          status: 1,
          message: "成功",
        });
      } catch (err) {
        res.cc(err);
      }
    } else {
      const updateContent = {
        name,
        sex,
        email,
        department,
        update_time,
        read_status: 0,
        read_list: null,
      };
      const sql = "UPDATE users SET ? WHERE id = ?";
      try {
        await db.query(sql, [updateContent, id]);
        res.send({
          status: 1,
          message: "成功",
        });
      } catch (err) {
        res.cc(err);
      }
    }
  },
  // 管理员权限变更
  updateIdentity: async (req, res) => {
    const { identity, id } = req.body;
    const sql = "UPDATE users SET identity = ? WHERE id = ? ";
    try {
      await db.query(sql, [identity, id]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 搜索用户
  searchUser: async (req, res) => {
    const { account, identity } = req.query;
    const sql = `SELECT * FROM users WHERE account = ? AND identity = ?`;
    const [data] = await db.query(sql, [account, identity]);
    res.send({
      status: 1,
      message: "成功",
      data: data,
    });
  },
  // 冻结状态更改
  banStatus: async (req, res) => {
    const { status, id } = req.body;
    const sql = "UPDATE users SET status = ? WHERE id = ?";
    try {
      await db.query(sql, [status, id]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取冻结用户列表
  getBanList: async (req, res) => {
    const { page } = req.query;
    const sql = `SELECT * FROM users WHERE status = '0' ORDER BY create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM users WHERE status = '0'`;
    const [data] = await db.query(sql);
    const [total] = await db.query(sql1);
    res.send({
      status: 1,
      message: "成功",
      data: data,
      total: total.length,
    });
  },
  // 根据部门筛选
  searchDepartment: async (req, res) => {
    const { page, department } = req.query;
    const sql = `SELECT * FROM users WHERE department = ? ORDER BY create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM users WHERE department = ?`;
    const [data] = await db.query(sql, department);
    const [total] = await db.query(sql1, department);
    res.send({
      status: 1,
      message: "成功",
      data: data,
      total: total.length,
    });
  },
  // 同时筛选冻结用户 部门
  searchDepartmentAndStatus: async (req, res) => {
    const { page, department } = req.query;
    const sql = `SELECT * FROM users WHERE department = ? AND status = '0' ORDER BY create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM users WHERE department = ?  AND status = '0'`;
    const [data] = await db.query(sql, department);
    const [total] = await db.query(sql1, department);
    res.send({
      status: 1,
      message: "成功",
      data: data,
      total: total.length,
    });
  },
  // 删除用户
  deleteUser: async (req, res) => {
    const { id, account } = req.body;
    const sql = "DELETE FROM users WHERE id = ?";
    await db.query(sql, id);
    const sql1 = "DELETE FROM image WHERE account = ?";
    await db.query(sql1, account);
    res.send({
      status: 1,
      message: "成功",
    });
  },
  // 部门设置
  updateDepartment: async (req, res) => {
    const { department } = req.body;
    const sql = "UPDATE company SET set_value = ? WHERE set_name = '部门设置' ";
    try {
      await db.query(sql, department);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 产品设置
  updateProduct: async (req, res) => {
    const { product } = req.body;
    const sql = "UPDATE company SET set_value = ? WHERE set_name = '产品设置' ";
    try {
      await db.query(sql, product);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取部门
  getDepartment: async (req, res) => {
    const sql = "SELECT * FROM company WHERE set_name = '部门设置' ";
    try {
      const [data] = await db.query(sql);
      res.send({
        status: 1,
        message: "成功",
        data: data[0].set_value,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取产品
  getProduct: async (req, res) => {
    const sql = "SELECT * FROM company WHERE set_name = '产品设置' ";
    try {
      const [data] = await db.query(sql);
      res.send({
        status: 1,
        message: "成功",
        data: data[0].set_value,
      });
    } catch (err) {
      res.cc(err);
    }
  },
};

module.exports = adminController;
